<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm
        v-if="formItemList.length"
        :formItemList="formItemList"
        :unfoldFlag="unfoldFlag"
        @search="search"
        @isUnfold="
          (v) => {
            unfoldFlag = v;
          }
        "
      >
      </SearchForm>
    </template>
    <template v-slot:table>
      <TableData
        :tableThead="tableThead"
        :dataList="dataList"
        :loading="loading"
      >
        <template v-slot:fbillflag="{ row }">
          <span>{{ queryfbillflagList(row.fbillflag) }}</span>
        </template>
        <template v-slot:seeDetail="{ row }">
          <span class="successText" @click="lookView(row)">查看详情</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination
        @current-change="handleCurrentChange"
        :page="page"
        :total="total"
      >
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <el-dialog
        title="详情"
        :visible.sync="dialogVisibleLookView"
        width="70%"
        :before-close="
          () => {
            dialogVisibleLookView = false;
          }
        "
      >
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column
            v-for="(item, index) in dialogtableThead"
            :key="index"
            :prop="item.prop"
            :label="item.label"
            min-width="100"
            align="center"
          >
          </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleLookView = false">关 闭</el-button>
        </span>
      </el-dialog>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from "@/components-production-management/Pagination";
import SearchForm from "@/components-production-management/SearchForm";
import TableData from "@/components-production-management/TableData";
//NC批次
import ncQueryApi from "@/api/nc-query/index";
import { fbillflagList } from "@/utils/NcQueryData";

export default {
  components: { MainSlot, Pagination, SearchForm, TableData },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      page: 1,
      total: 10,
      searchFormData: {},
      dataList: [],
      dialogVisibleLookView: false,
      tableData: [],
      dialogtableThead: [
        {
          label: "单位名称",
          prop: "castunitname",
        },
        {
          label: "物料编码名称",
          prop: "cmaterialvname",
        },
        {
          label: "主单位名称",
          prop: "cunitname",
        },
        {
          label: "实发辅数量",
          prop: "nassistnum",
        },
        {
          label: "实发主数量",
          prop: "nnum",
        },
        {
          label: "应发辅数量",
          prop: "nshouldassistnum",
        },
        {
          label: "应发主数量",
          prop: "nshouldnum",
        },
        {
          label: "批次号",
          prop: "vbatchcode",
        },
        {
          label: "条数",
          prop: "vbdef1",
        },
        {
          label: "均重:数量/条数",
          prop: "vbdef2",
        },
        {
          label: "品级名称",
          prop: "vbdef5name",
        },
        {
          label: "销售品规名称",
          prop: "vbdef6name",
        },
        {
          label: "换算率",
          prop: "vchangerate",
        },
        {
          label: "活鱼品规名称",
          prop: "vfree1name",
        },
      ],
      tableThead: [
        // { isShow: true, label: '批次编号', prop: 'batchName' },
        {
          isShow: true,
          label: "部门名称",
          prop: "cdptname",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "调出库存组织名称",
          prop: "cothercalbodyoname",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "调出仓库名称",
          prop: "cotherwhname",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "仓库名称",
          prop: "cwarehousename",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "单据日期",
          prop: "dbilldate",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "单据状态",
          slotName: "fbillflag",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "总数量",
          prop: "ntotalnum",
          minWidth: "120px",
        },

        {
          isShow: true,
          label: "库存组织",
          prop: "pk_org",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "单据号",
          prop: "vbillcode",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "出入库类型编码",
          prop: "vtrantypecode",
          minWidth: "120px",
        },
        {
          isShow: true,
          label: "查看详情",
          slotName: "seeDetail",
          minWidth: "120px",
        },
      ],

      formItemList: [
       {
          label: "部门名称",
          type: "select",
          selectOptions: this.setOptions("cdptid"),
          param: "cdptid",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
            {
          label: "调出仓库名称",
          type: "input",
          param: "cotherwhname",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
        {
          label: "单据状态",
          type: "select",
          selectOptions: [
            { text: "自由", value: 1 },
            { text: "审批通过", value: 2 },
            { text: "冻结", value: 3 },
            { text: "关闭", value: 4 },
            { text: "审批中", value: 7 },
            { text: "审批不通过", value: 8 },
            { text: "失效", value: 5 },
          ],
          param: "fbillflag",
          defaultSelect: "", // 下拉框默认选中项
          multiple: false,
          isShow: true,
        },
        {
          label: "日期查询",
          type: "datetimerange",
          param: "dateTime",
          multiple: false,
          isShow: true,
        },
      ],
    };
  },
  mounted() {
    this.queryPageByCondition();
  },
  methods: {
      setOptions(param) {
      let options = [];
      if (param == "cdptid") {
        let list =
          JSON.parse(localStorage.getItem("NcOrgDept")) ||
          this.$store.state.NcBasicInfo.OrgDept;
        list.forEach((item) => {
          options.push({ text: item.name, value: item.pk_dept });
        });
      } else if (param == "pk_org") {
        let list =
          JSON.parse(localStorage.getItem("NcOrgOrgs")) ||
          this.$store.state.NcBasicInfo.OrgOrgs;
        list.forEach((item) => {
          options.push({ text: item.name, value: item.pk_org });
        });
      }
      return options;
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: 10,
        entity: {
          ...this.searchFormData,
        },
      };
      this.loading = true;
      ncQueryApi
        .queryTransin(params)
        .then((res) => {
          this.loading = false;
          if (res.success == 1) {
            this.dataList = res.data.content || [];
            this.total = res.data.totalElements;
            if (res.data.totalPages && this.page > res.data.totalPages) {
              this.page = res.data.totalPages;
              this.queryPageByCondition();
            }
          } else {
            this.$message.warning(res.message || "分页查询失败");
          }
        })
        .catch((err) => {
          this.loading = false;
          console.log(err);
        });
    },
    search(data) {
      this.page = 1;
      if (data.dateTime && data.dateTime.length) {
        data.startDate = data.dateTime[0];
        data.endDate = data.dateTime[1];
      }
      delete data.dateTime;
      this.searchFormData = { ...data };

      for (let key in this.searchFormData) {
        if (!this.searchFormData[key]) {
          this.searchFormData[key] = null;
        }
      }
      console.log(this.searchFormData, data, "this.searchFormData");
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },

    //查看详情
    async lookView(row) {
      let obj = JSON.parse(JSON.stringify(row));
      let res = await ncQueryApi.queryTransinDtl({
        orderId: obj.cgeneralhid,
      });
      if (res.success) {
        this.tableData = res.data || [];
        this.dialogVisibleLookView = true;
      }
    },
    queryfbillflagList(e) {
      return fbillflagList(e);
    },
  },
};
</script>
<style lang="scss" scoped></style>